package com.google.code.hostsman;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;

import com.google.code.hostsman.NewGroupDialogFragment.EditGroupDialogListener;
import com.google.code.hostsman.db.DataSource;
import com.google.code.hostsman.db.Group;

public class MainActivity extends FragmentActivity implements
		EditGroupDialogListener {

	private DataSource datasource;
	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		datasource = new DataSource(this);
		datasource.open();

		List<Group> values = datasource.getAllGroups();

		// use the SimpleCursorAdapter to show the
		// elements in a ListView
		// ArrayAdapter<Group> adapter = new
		// ArrayAdapter<Group>(this,android.R.layout.simple_list_item_checked,
		// values);
		final GroupArrayAdapter adapter = new GroupArrayAdapter(this, values);
		listView = (ListView) findViewById(android.R.id.list);

		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, final View view,
					int position, long id) {

				Group item = (Group) parent.getItemAtPosition(position);

				item.setEnabled(!item.isEnabled());
				datasource.updateGroup(item);

				CheckedTextView textview = (CheckedTextView) view;
				textview.setChecked(item.isEnabled());

				adapter.notifyDataSetChanged();

			}
		});
		
		listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> parent, View view,
			          int position, long id) {
				
				Group group= (Group) listView.getAdapter().getItem(position);
				System.out.println(id+" "+group.getId());
				
				Intent intent=new Intent(MainActivity.this, GroupActivity.class);
				intent.putExtra(GroupActivity.INTENT_GROUP_ID,group.getId());
				startActivity(intent);
				
				return true;
			}
		});
	}

	private class GroupArrayAdapter extends ArrayAdapter<Group> {
		private List<Group> values;
		private Context context;

		public GroupArrayAdapter(Context context, List<Group> values) {
			super(context, android.R.layout.simple_list_item_checked, values);
			this.context = context;
			this.values = values;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View rowView = LayoutInflater.from(context).inflate(
					android.R.layout.simple_list_item_checked, parent, false);
			CheckedTextView textView = (CheckedTextView) rowView
					.findViewById(android.R.id.text1);
			textView.setText(values.get(position).getName());
			textView.setChecked(values.get(position).isEnabled());
			return rowView;
		}

		public long getItemId(int position) {
			return values.get(position).getId();
		}

		public boolean hasStableIds() {
			return true;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	// Will be called via the onClick attribute
	// of the buttons in main.xml
	public void onClick(View view) {
		@SuppressWarnings("unchecked")
		ArrayAdapter<Group> adapter = (ArrayAdapter<Group>) listView
				.getAdapter();
		Group group = null;
		switch (view.getId()) {
		case R.id.delete:
			if (listView.getAdapter().getCount() > 0) {
				group = (Group) listView.getAdapter().getItem(0);
				datasource.deleteGroup(group.getId());
				adapter.remove(group);
			}
			break;
		}
		adapter.notifyDataSetChanged();
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.action_new_group:
	    	showNewGroupDialog();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}

	public void showNewGroupDialog() {
		NewGroupDialogFragment fragment = new NewGroupDialogFragment();
		fragment.show(getSupportFragmentManager(), "new_group");
	}

	@Override
	protected void onResume() {
		datasource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		datasource.close();
		super.onPause();
	}

	@Override
	public void onDialogGroupAdd(DialogFragment dialog, Group group) {
		// save the new comment to the database
		if (group.getName().length() > 0) {
			datasource.createGroup(group);
			GroupArrayAdapter adapter = (GroupArrayAdapter) listView
					.getAdapter();
			adapter.add(group);
			
			Intent intent=new Intent(this, GroupActivity.class);
			intent.putExtra(GroupActivity.INTENT_GROUP_ID, group.getId());
			startActivity(intent);
		}
	}
}